package com.example.myapplication5.views.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * 可缩放、可拖拽文字的TextView
 */
@SuppressLint("AppCompatCustomView")
public class ZoomableTextView extends TextView {
    // 矩阵用于变换
    private Matrix matrix = new Matrix();
    private Matrix savedMatrix = new Matrix();

    // 手势模式
    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;

    // 触摸点记录
    private PointF start = new PointF();
    private PointF mid = new PointF();
    private float oldDist = 1f;
    private float[] matrixValues = new float[9];

    // 缩放相关配置
    private float minScale = 0.5f;   // 最小缩放比例
    private float maxScale = 3.0f;   // 最大缩放比例
    private float originalTextSize;  // 初始文字大小

    public ZoomableTextView(Context context) {
        super(context);
        init();
    }

    public ZoomableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ZoomableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        originalTextSize = getTextSize();
        setClickable(true);
        // 初始矩阵为单位矩阵
        matrix.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 处理触摸事件
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // 单指按下，准备拖动
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                mode = DRAG;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                // 双指按下，准备缩放
                oldDist = spacing(event);
                if (oldDist > 10f) {  // 确保是双指操作
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                }
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                // 手指抬起，结束手势
                mode = NONE;
                break;

            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // 处理拖动
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                } else if (mode == ZOOM) {
                    // 处理缩放
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;

                        // 获取当前缩放比例
                        matrix.getValues(matrixValues);
                        float currentScale = matrixValues[Matrix.MSCALE_X];

                        // 限制缩放范围
                        if (scale * currentScale > maxScale) {
                            scale = maxScale / currentScale;
                        } else if (scale * currentScale < minScale) {
                            scale = minScale / currentScale;
                        }

                        // 以双指中点为中心进行缩放
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
        }

        // 重绘以应用变换
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 保存画布状态
        canvas.save();
        // 应用矩阵变换
        canvas.concat(matrix);
        // 绘制文本
        super.onDraw(canvas);
        // 恢复画布状态
        canvas.restore();
    }

    /**
     * 计算两点之间的距离
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    /**
     * 计算两点的中点
     */
    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

    /**
     * 重置到初始状态
     */
    public void reset() {
        matrix.reset();
        invalidate();
    }

    /**
     * 设置最大缩放比例
     */
    public void setMaxScale(float maxScale) {
        this.maxScale = maxScale;
    }

    /**
     * 设置最小缩放比例
     */
    public void setMinScale(float minScale) {
        this.minScale = minScale;
    }

    /**
     * 获取当前缩放比例
     */
    public float getCurrentScale() {
        matrix.getValues(matrixValues);
        return matrixValues[Matrix.MSCALE_X];
    }
}